<?php

session_start();
$_SESSION['current_fitting'] = NULL;



function getJS() {
	?>
	<script type="text/javascript">
		function fetchFittings(desc)
		{
		var xmlhttp = null;
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    document.getElementById("base_fit").innerHTML=xmlhttp.responseText;
		    }
		  }
		xmlhttp.open("GET","getFitting.php?fitting_desc="+desc+"&"+Math.random(), true);
		xmlhttp.send();
		}
		</script>
	
	<?
	
}





function getFittingSelection() {
	
	$fittings_content = file_get_contents('fittings_list.dat', true);
	$current_fitting = $_SESSION['current_fitting'];
	#$fittings_file = "fittings_list.dat";
	#$fittings_content = file($fittings_file);

	//put content in array
	$fittings = explode("<EndOfFitting>\n", $fittings_content);

	$form_content = "<select style='font-size: 9pt' name='fitting_list' id='fitting_list' onchange=fetchFittings(this.value)>";
	
	$form_content .= ("<option>Manually entered fitting...</option>\n");
	foreach ($fittings as $this_fitting) {
		$lines = explode("\n", $this_fitting);
		$title = $lines[0];
		if (strcmp($title, $current_fitting) == 0) {
			$form_content .= ("<option selected>".$title."</option>\n");
		} else {
			$form_content .= ("<option>".$title."</option>\n");
		}		
	}
	$form_content .= "</select>";
	$fittings_list = $form_content;
	return $fittings_list;
	
	
}

function getBody($report) {
	?>
	<html>
		<head>
			<link rel="stylesheet" type="text/css" href="http://kbb.tgrads.com//themes/default/default.css" />
			<link rel="stylesheet" type="text/css" href="styles/main.css" />
			<title>Check yer fitting tool!</title>
			
			<? getJS(); ?>
		</head>
		<body>
			
			
		<div align="center" id="popup" style="display:none;
		position:absolute;
	    top:217px; width:99%;
		z-index:3;
	    padding: 5px;"></div>
		
		<table class="main-table" align="center" bgcolor="#111111" border="0" cellspacing="1" style="height: 100%">
		<tr style="height: 100%">
		
		<td valign="top" height="100%" style="height: 100%">
		<div id="header">
		<a href="?a=home"><img src="http://kbb.tgrads.com//banner/kb_header3.jpg" border="0" alt="Banner"  /></a>
		</div>
		<table class="navigation" width="100%" style="height:25px;" border="0" cellspacing="1">
		<tr class="kb-table-row-odd">
		<td width="20%" align="center"><a class="link" style="display: block;" href="index.php" target="_self">Home</a></td>
		<td width="20%" align="center"><a class="link" style="display: block;" href="http://kbb.tgrads.com" target="_self">Killboard</a></td>
		<td width="20%" align="center"><a class="link" style="display: block;" href="index.php?q=ignored" target="_self">View Ignored</a></td>
		<td width="20%" align="center"><a class="link" style="display: block;" href="checkFitting.php" target="_self">Check Fitting</a></td>
		<td width="20%" align="center"><a class="link" style="display: block;" href="index.php?q=about" target="_self">About</a></td>
		</tr>
		</table>
		<table class="navigation" width="100%" style="height:25px;" border="0" cellspacing="1">
		
		<tr class="kb-table-row-odd">
		<td width="32%" align="center"><a class="link" style="display: block;" href="admin.php" target="_self">Review</a></td>
		<td width="32%" align="center"><a class="link" style="display: block;" href="configure.php" target="_self">Configure</a></td>
		<td width="32%" align="center"><a class="link" style="display: block;" href="users.php" target="_self">User Manager</a></td>
		</tr>
		</table>
		
		<center>
		<br><br> 

		<table>
			<tr>
				<td colspan=2>
					<p class="box_header"><font color="red">Warning:</font> Does not work on IE... yet.</p>
				</td>
			</tr>
			<tr>
				<td>
					<? if ($_SESSION['current_fitting'] != NULL) { ?>
						<p class="box_header"><font color="orange">Fitting Report:</font></p> 
						<p class="report_body"><? echo $report; ?></p>
					<? } ?>
					<p class="box_header"><font color="orange">IMPORTANT:</font> Copy and Paste the "EFT Fitting" associated with the killmail exactly as it appears on the killboard. Note that this is not the KM itself.</p>
					<form action="checkFitting.php" method="post" name="fittings_form">
						<table>
							<tr>
								<td>
									<p class="box_header">Base Fit:
									<? echo getFittingSelection(); ?>
									</p>
								</td>
								<td>
									<p class="box_header">My Fit:</p>
								</td>
							</tr>
							<tr>
								<td>
									<textarea rows="25" cols="60" id="base_fit" name="base_fit" class="inputtext"><? echo stripslashes($_POST['base_fit']); ?>
									</textarea></br>
								</td>
								<td>
									<textarea rows="25" cols="60" id="my_fit" name="my_fit" class="inputtext"><? echo stripslashes($_POST['my_fit']); ?>
									</textarea></br>
								</td>
							</tr>
							<tr>
								<td>
									&nbsp;
								</td>
								<td>
									<input type="submit" value="Check It!" />
								</td>
							</tr>
						</table>
						
						
					</form>
				</td>
			</tr>
		</table>

		</body>
	</html>
	
	<?
	
}




function getModOnly($this_mod) {
	$items = explode(",", $this_mod);
	return trim($items[0]);
	
}

function getFit($this_fit) {
	# print $this_fit;
	$pieces = explode("\n", $this_fit);
	$fitting = array(0 => array(), 	# title
					1 => array(), 	# low
					2 => array(),	# mid
					3 => array(),	# high
					4 => array(),	# rigs
					5 => array());	# drones
	$i = 0;
	$this_index = 1;
	$title = $pieces[0];
	
	array_push($fitting[$i], $title);
	#echo "Title:", $title;
	foreach($pieces as $raw_piece) {
		
		#echo strlen($piece), ","; 
		if (strlen($raw_piece) == 1) {
			$this_index++;
		} else {
			if ($i > 0) {
				$piece = getModOnly($raw_piece);
				array_push($fitting[$this_index], trim($piece));
			}
		}
		$i++;
	}
	#print_r($pieces);
	#print_r($fitting);
	return $fitting;
}




function outputFit($this_fit) {
	$title 	= $this_fit[0];
	$lows 	= $this_fit[1];
	$mids 	= $this_fit[2];
	$highs 	= $this_fit[3];
	$rigs 	= $this_fit[4];
	$drones = $this_fit[5];
	
	echo "Title: ", $title[0];
	echo "Lows: ", implode(",", $lows);
	
}


function wrapNotice($mods, $flag) {
	# 0 - "OK!" 
	# 1 - "Missing!"
	# 2 - "Not Approved!"
	
	$flag_map = array(
		0 => "<font color='green'>OK!</font>",
		1 => "<font color='yellow'>Missing!</font>",
		2 => "<font color='red'>Not Approved!</font>"
	);
	
	$report = "";
	foreach ($mods as $mod) {
		if (strlen($mod) > 0) {
			$report = sprintf("%s%s - %s<br>", $report, $mod, $flag_map[$flag]);
		}
	}
	return $report;
}

function wrapNotice2($mods, $flag) {
	$report = "";
	foreach ($mods as $mod) {
		if (strlen($mod) > 0) {
			$report = sprintf("%s%s<img src=\"%s\"><br>", $report, $mod, $flag);
		}
	}
	echo $report;
}

function tallyMods($base_mods, $my_mods) {
	# array_intersect and array_diff don't work cause they can count 2 in one
	# list with 1 in the other as complete overlap
	$base_len 	= sizeof($base_mods);
	$my_len		= sizeof($my_mods);
	
	$common_mods = array();
	
	for ($i = 0; $i < $base_len; $i++) {
		for ($j = 0; $j < $my_len; $j++) {
			#printf("Comparing %s (%d) and %s (%d)<br>", $base_mods[$i], $i, $my_mods[$j], $j);
			if (($base_mods[$i] != NULL) and
				($my_mods[$j] != NULL) and
				(strcmp($base_mods[$i], $my_mods[$j]) == 0)) {
					
				array_push($common_mods, $base_mods[$i]);
				$base_mods[$i] = NULL;
				$my_mods[$j] = NULL;
				break;
			}
		}		
	}

	$report = wrapNotice($common_mods, 0);
	$report .= wrapNotice($base_mods, 1);
	$report .= wrapNotice($my_mods, 2);
	return $report;
}

function generateReport($base_fit, $my_fit) {
	$base_title = $base_fit[0];
	$base_lows 	= $base_fit[1];
	$base_mids 	= $base_fit[2];
	$base_highs = $base_fit[3];
	$base_rigs 	= $base_fit[4];
	$base_drones = $base_fit[5];
	
	$my_title 	= $my_fit[0];
	$my_lows 	= $my_fit[1];
	$my_mids 	= $my_fit[2];
	$my_highs 	= $my_fit[3];
	$my_rigs 	= $my_fit[4];
	$my_drones 	= $my_fit[5];	
	
	$tally_lows = tallyMods($base_lows, $my_lows);
	$tally_mids = tallyMods($base_mids, $my_mids);
	$tally_highs = tallyMods($base_highs, $my_highs);
	$tally_rigs = tallyMods($base_rigs, $my_rigs);
	
	$report = stripslashes(sprintf("	<b>Low slots:</b><br>%s<br>
						<b>Middle slots:</b><br>%s<br>
						<b>High slots:</b><br>%s<br>
						<b>Rigs:</b><br>%s<br>", 
						$tally_lows, $tally_mids, $tally_highs, $tally_rigs));
	return $report;
	
	
}

if (isset($_POST['base_fit']) == true) {
	$base_fit = getFit($_POST['base_fit']);
	#print_r( $base_fit);
	#outputFit($base_fit);
}

if (isset($_POST['my_fit']) == true) {
	$my_fit = getFit($_POST['my_fit']);
}


if (isset($_POST['fitting_list']) == true) {
	$_SESSION['current_fitting'] = $_POST['fitting_list'];
}


$report = generateReport($base_fit, $my_fit);

getBody($report);


?>